/**
 * Created with IntelliJ IDEA.
 * User: Lorenzo hee
 * Date: 14-12-28
 * Time: 下午5:43
 * login js
 */

$(function(){

    $('.rem').click(function () {
        $(this).toggleClass('selected');
    });

    $('#signup_select').click(function () {
        $('.form_row ul').show();
        event.cancelBubble = true;
    });

    $('#d').click(function () {
        $('.form_row ul').toggle();
        event.cancelBubble = true;
    });

    $('body').click(function () {
        $('.form_row ul').hide();
    });

    $('.form_row li').click(function () {
        var v = $(this).text();
        $('#signup_select').val(v);
        $('.form_row ul').hide();
    });
    $('#name_valid').hide();
    $('.valid_loading_name').hide();
    $('#signup_name').bind('input propertychange', function() {
        $('.valid_loading_name').show();
        $.ajax({
            type: "POST",
            url: "rbac/cmUser?method=getUserName",
            data: "uname=" + $('#signup_name').val(),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function(data) {
                if(data["valid"]=="true"){
                    $('.valid_loading_name').hide();
                    $('#name_valid').removeClass("error").addClass("ok");
                    $('#name_valid').show();
                } else {
                    $('.valid_loading_name').hide();
                    $('#name_valid').removeClass("ok").addClass("error");
                    $('#name_valid').show();
                }
            },
            error: function(XMLResponse){
                $('.valid_loading_name').hide();
                $('#name_valid').removeClass("ok").addClass("error");
                $('#name_valid').show();
            }
        })
    })
    function validaSubmit() {
        if($('#name_valid').hasClass('error')) {
            $('#errorMessage').text("请输入正确的用户名。");
            return false;
        }
        if($('#signup_name').val()=='') {
            $('#errorMessage').text("用户名不能为空。");
            return false;
        }
        if($('#signup_password').val()=='') {
            $('#errorMessage').text("密码不能为空。");
            return false;
        }
        return true;
    }
    $('.login-btn').click(function() {
        login();
    })

    function login() {
        if(!validaSubmit()){
            return;
        }
        $.ajax({
            type: "POST",
            url: "rbac/cmUser?method=login",
            data: "uname=" + $('#signup_name').val() + "&pwd=" + hex_md5($('#signup_password').val()).toUpperCase(),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function(data) {
                if(data["valid"] === 1) {
                    window.location = "main.html"
                } else if (data["valid"] === 2) {
                    $('#errorMessage').text("您使用的用户已失效，请联系系统管理员");
                    $('#signup_password').val('');
                    return;
                } else {
                    $('#errorMessage').text("您输入的用户名或密码有误。")
                    $('#signup_password').val('');
                    return;
                }
            },
            error: function() {
                $('#errorMessage').text("无法登陆，请重新输入。")
                $('#signup_password').val('');
                return;
            }
        })
    }

    //listen the enter button
    $(document).keypress(function(event) {
        var key = event.which;
        if(key==13) {
            login();
        }
    })
})